<template>
  <a-modal
    :title="$t('安全设置')"
    :width="width"
    :visible="visible"
    :cancelText="$t('关闭')"
    @cancel="handleCancel"
    v-drag
  >
    <template slot="footer">
      <a-button key="back" @click="handleCancel">{{ $t('关闭') }}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-row>
        <a-col style="color: orange">{{ $t('说明：请根据场地的运营需要，设置场地吧台的安全参数') }}</a-col>
      </a-row>
      <a-row :gutter="[0, 16]" style="padding: 20px 30px 0">
        <a-col :span="24">
          <a-checkbox
            :checked="barReaderControl.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barReaderControl', 'ivalue', 1, 'autoSave')"
            >{{ $t('宝盒启动') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{ $t('开启宝盒启动功能后，吧台不连接宝盒无法正常启动') }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barAccountLogin.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barAccountLogin', 'ivalue', 1, 'autoSave')"
            >{{ $t('账号密码登录') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{ $t('关闭此功能则吧台将不支持账号密码登录') }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barImageVerify.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barImageVerify', 'ivalue', 1, 'autoSave')"
            >{{ $t('图文验证') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{ $t('开启后吧台在使用账号密码登录时需要进行图文验证') }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barCardLoginPwd.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barCardLoginPwd', 'ivalue', 1, 'autoSave')"
            >{{ $t('授权卡密码验证') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t('关闭后吧台刷授权卡无需输入密码可直接登录')
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barIPAuth.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barIPAuth', 'ivalue', 1, 'autoSave')"
            >{{ $t('局域网IP变更') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t(
                '启用后若吧台登录时的局域网IP与上一次登录时的不同则需要管理员认证才能登录；建议吧台使用固定IP并启用此功能'
              )
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barWIPAuth.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barWIPAuth', 'ivalue', 1, 'autoSave')"
            >{{ $t('外网IP变更') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t(
                '开启后若吧台登录时的外网IP与上一次登录时的不同则需要管理员认证才能登录；建议场地使用网络专线时开启此功能'
              )
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barIPBind.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barIPBind', 'ivalue', 1, 'autoSave')"
            >{{ $t('局域网IP锁定') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t('开启后请为每一个吧台设定所属局域网IP；若登录时的局域网IP与所设定的不一致则禁止本次登录')
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barMACBind.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barMACBind', 'ivalue', 1, 'autoSave')"
            >{{ $t('MAC地址锁定') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t('开启后请为每一个吧台设定所属MAC地址；若登录时的MAC地址与所设定的不一致则禁止本次登录')
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
        <a-col :span="24">
          <a-checkbox
            :checked="barWeakPwd.ivalue == 1"
            @change="(e) => changeSelectState(e, 'barWeakPwd', 'ivalue', 1, 'autoSave')"
            >{{ $t('非安全密码强制修改') }}</a-checkbox
          ><a-tooltip>
            <template slot="title">{{
              $t('开启后吧台使用账号密码登录时，简单密码不允许登录，必须要强制修改为复杂密码后才允许登录吧台')
            }}</template>
            <a-icon type="question-circle" />
          </a-tooltip>
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'
let that
export default {
  name: 'SecuritySettingsModal',
  data() {
    return {
      width: 600,
      visible: false,
      confirmLoading: false,
      barReaderControl: {
        name: 'barReaderControl',
        ivalue: 1,
        comment: '吧台-无宝盒不允许启用',
      },
      barAccountLogin: {
        name: 'barAccountLogin',
        ivalue: 1,
        comment: '吧台-启用账号密码登录',
      },
      barImageVerify: {
        name: 'barImageVerify',
        ivalue: 0,
        comment: '吧台-启用登录图文认证',
      },
      barCardLoginPwd: {
        name: 'barCardLoginPwd',
        ivalue: 1,
        comment: '吧台-启用授权卡登录密码验证',
      },
      barIPAuth: {
        name: 'barIPAuth',
        ivalue: 1,
        comment: '吧台-启用局域网IP变更',
      },
      barWIPAuth: {
        name: 'barWIPAuth',
        ivalue: 0,
        comment: '吧台-启用外网IP变更',
      },
      barIPBind: {
        name: 'barIPBind',
        ivalue: 0,
        comment: '吧台-启用局域网IP锁定',
      },
      barMACBind: {
        name: 'barMACBind',
        ivalue: 0,
        comment: '吧台-启用MAC地址锁定',
      },
      barWeakPwd: {
        name: 'barWeakPwd',
        ivalue: 0,
        comment: '吧台-非安全密码强制修改',
      },
    }
  },
  mounted() {
    that = this
  },
  methods: {
    show() {
      that.visible = true
      that.getParameter('barReaderControl', 'ivalue', 1)
      that.getParameter('barAccountLogin', 'ivalue', 1)
      that.getParameter('barImageVerify', 'ivalue', 0)
      that.getParameter('barCardLoginPwd', 'ivalue', 1)
      that.getParameter('barIPAuth', 'ivalue', 1)
      that.getParameter('barWIPAuth', 'ivalue', 0)
      that.getParameter('barIPBind', 'ivalue', 0)
      that.getParameter('barMACBind', 'ivalue', 0)
      that.getParameter('barWeakPwd', 'ivalue', 0)
    },
    getParameter(name, defKey, defValue) {
      getAction('/bis/bisSysParameter/list', { name: name }).then((res) => {
        if (res.success && res.result.records.length > 0) {
          that.$set(that, name, res.result.records[0])
        } else {
          that.$set(that[name], defKey, defValue)
        }
      })
    },
    close() {
      that.$emit('close')
      that.visible = false
    },
    handleCancel() {
      that.close()
    },
    changeSelectState(e, name, key, type, arg) {
      if (type == 1) {
        that.$set(that[name], key, e.target.checked ? 1 : 0)
      }
      if (type == 2) {
        that.$set(that[name], key, e.target.checked ? 'Y' : 'N')
      }
      if (arg == 'autoSave') {
        that.parameterChange(name)
      }
    },
    parameterChange(name) {
      let querydata = {}
      let url = '',
        method = ''
      if (that[name].id) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      querydata = Object.assign({}, that[name])
      httpAction(url, querydata, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.getParameter(name)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
  },
}
</script>

<style>
</style>